package com.franklin.java.api.action;

import com.franklin.plugins.common.utils.ReflectionUtils;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.util.NlsActions;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Action抽象父类
 * @author Franklin
 * @since 2021/9/6 17:24
 */
public abstract class AbstractAction<T extends PsiElement> extends AnAction {

    protected Class<?> typeClass;

    public AbstractAction(@Nullable @NlsActions.ActionText String text, @Nullable @NlsActions.ActionDescription String description, @Nullable Icon icon) {
        super(text, description, icon);
        typeClass = ReflectionUtils.getSuperClassGenericType(this.getClass(),0);
    }

    /**
     * 触发条件<br>
     * @author Franklin
     * @since 2021/9/6 17:26
     * @param e:
     * @param visible:
     * @return void
     */
    protected void setActionPresentationVisible(AnActionEvent e, boolean visible) {
        e.getPresentation().setVisible(visible);
    }

    protected T getObject(AnActionEvent e){
        PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
        if (typeClass.isInstance(psiElement)){
            return (T) psiElement;
        }
        return null;
    }

}
